<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
/**
 * HTML View class for the HelloWorld Component
 */
class ShopViewSearch extends JView
{
	protected $item = null;
	protected $path_img = null;
	// Overwriting JView display method
	function display($tpl = null) 
	{
		$app = JFactory::getApplication();
		$this->path_img = JURI::root().'images/image_product/';
		$giaothuong = JRequest::getVar('giaothuong');
		$this->abc = $giaothuong;
		switch ($giaothuong) {
			case 'congty':
				$keyword = JRequest::getVar('keyword','');
				$catid = JRequest::getVar('catid','');
				$address = JRequest::getVar('address','');
				
				$db = JFactory::getDBO();
				$query = $db->getQuery(true);
				
				// Check for errors.
				if (count($errors = $this->get('Errors'))) 
				{
					JError::raiseError(500, implode('<br />', $errors));
					return false;
				}
				$query->clear();
				$query->select('count(*)');
				$query->from('#__shop_product');
				if($keyword != ''){
					$search = $db->Quote('%'.$db->getEscaped($keyword, true).'%');
					$query->where('vtitle LIKE '.$search);	
				}
				if($catid != ''){
					$query->where('catid = '.$catid);
				}
				if($address != ''){
					$query->where('address_id = '.$address);
				}
				
				$query->where('state = 1');
				
				$db->setQuery((string)$query);
				$total = $db->loadResult();
				
				$limit = JRequest::getVar('limit',$app->getCfg('list_limit')); 
				$limitstart = JRequest::getVar('limitstart', 0);
				jimport('joomla.html.pagination');
				$this->pagination  = new JPagination($total, $limitstart, $limit);
				
				$query->clear();
				$query->select('*');
				$query->from('#__shop_product');
				if($keyword != ''){
					$search = $db->Quote('%'.$db->getEscaped($keyword, true).'%');
					$query->where('vtitle LIKE '.$search);	
				}
				if($catid != ''){
					$query->where('catid = '.$catid);
				}
				if($address != ''){
					$query->where('address_id = '.$address);
				}
				$query->where('state = 1');
				
		
				$db->setQuery((string)$query,$this->pagination->limitstart,$this->pagination->limit);
				$searchs = $db->loadObjectList();
				$this->total = $total;
				$this->items = $searchs;
				// Display the view
				parent::display($tpl);
				break;
			case 'giaothuong':
				$keyword = JRequest::getVar('keyword','');
				$catid = JRequest::getVar('catid','');
				$address = JRequest::getVar('address','');
				$company = JRequest::getVar('company','');
				
				$db = JFactory::getDBO();
				$query = $db->getQuery(true);
				
				// Check for errors.
				if (count($errors = $this->get('Errors'))) 
				{
					JError::raiseError(500, implode('<br />', $errors));
					return false;
				}
				$query->clear();
				$query->select('count(*)');
				$query->from('#__shop_method');
				if($keyword != ''){
					$search = $db->Quote('%'.$db->getEscaped($keyword, true).'%');
					$query->where('title LIKE '.$search);	
				}
				if($catid != ''){
					$query->where('catid = '.$catid);
				}
				if($address != ''){
					$query->where('address_id = '.$address);
				}
				if($company != ''){
					$query->where('company = '.$company);
				}
				
				$query->where('state = 1');
				
				$db->setQuery((string)$query);
				$total = $db->loadResult();
				
				$limit = JRequest::getVar('limit',$app->getCfg('list_limit')); 
				$limitstart = JRequest::getVar('limitstart', 0);
				jimport('joomla.html.pagination');
				$this->pagination  = new JPagination($total, $limitstart, $limit);
				
				$query->clear();
				$query->select('*');
				$query->from('#__shop_method');
				if($keyword != ''){
					$search = $db->Quote('%'.$db->getEscaped($keyword, true).'%');
					$query->where('title LIKE '.$search);	
				}
				if($catid != ''){
					$query->where('catid = '.$catid);
				}
				if($address != ''){
					$query->where('address_id = '.$address);
				}
				if($company != ''){
					$query->where('company = '.$company);
				}
				$query->where('state = 1');
				
		
				$db->setQuery((string)$query,$this->pagination->limitstart,$this->pagination->limit);
				$searchs = $db->loadObjectList();
				$this->total = $total;
				$this->items = $searchs;
				// Display the view
				parent::display($tpl);
				break;
			case 'tuyendung':
				$keyword = JRequest::getVar('keyword','');
				$catid = JRequest::getVar('catid','');
				$address = JRequest::getVar('address','');
				$company = JRequest::getVar('company','');
				
				$db = JFactory::getDBO();
				$query = $db->getQuery(true);
				
				// Check for errors.
				if (count($errors = $this->get('Errors'))) 
				{
					JError::raiseError(500, implode('<br />', $errors));
					return false;
				}
				$query->clear();
				$query->select('count(*)');
				$query->from('#__shop_employer');
				if($keyword != ''){
					$search = $db->Quote('%'.$db->getEscaped($keyword, true).'%');
					$query->where('title LIKE '.$search);	
				}
				if($catid != ''){
					$query->where('catid = '.$catid);
				}
				if($address != ''){
					$query->where('address_id = '.$address);
				}
				if($company != ''){
					$query->where('company = '.$company);
				}
				
				$query->where('state = 1');
				
				$db->setQuery((string)$query);
				$total = $db->loadResult();
				
				$limit = JRequest::getVar('limit',$app->getCfg('list_limit')); 
				$limitstart = JRequest::getVar('limitstart', 0);
				jimport('joomla.html.pagination');
				$this->pagination  = new JPagination($total, $limitstart, $limit);
				
				$query->clear();
				$query->select('*');
				$query->from('#__shop_employer');
				if($keyword != ''){
					$search = $db->Quote('%'.$db->getEscaped($keyword, true).'%');
					$query->where('title LIKE '.$search);	
				}
				if($catid != ''){
					$query->where('catid = '.$catid);
				}
				if($address != ''){
					$query->where('address_id = '.$address);
				}
				if($company != ''){
					$query->where('company = '.$company);
				}
				$query->where('state = 1');
				
		
				$db->setQuery((string)$query,$this->pagination->limitstart,$this->pagination->limit);
				$searchs = $db->loadObjectList();
				$this->total = $total;
				$this->items = $searchs;
				// Display the view
				parent::display($tpl);
				break;
		}
		
		
	}
	/**
	* Get Number Word in longtext
	*
	**/
	public function getText(&$longText,$numWords){
		$ret="";
		if($longText!=""){
			$longText=trim($longText);
			$longText=stripslashes($longText);
			$longText=strip_tags($longText);
			$arrayText=explode(" ",$longText);
			if(count($arrayText)>$numWords){
				for($i=0;$i<$numWords;$i++){
					$ret.=$arrayText[$i]." ";
				}
				$longText=trim($ret)."..";
				return $longText;
			}
			else{
				return $longText;
			}
		}
	}
}